package com.qidian.buy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qidian.buy.entity.Specifications;
import com.qidian.buy.entity.SpecificationsItem;
import com.qidian.buy.mapper.SpecificationsItemMapper;
import com.qidian.buy.mapper.SpecificationsMapper;
import com.qidian.buy.service.ISpecificationsService;
import com.qidian.buy.util.pojo.LayuiTableDTO;
import com.qidian.buy.util.pojo.QDResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author 郭浩
 */
@Service
public class SpecificationsServiceImpl implements ISpecificationsService {
    @Resource
    private SpecificationsMapper specificationsMapper;
    @Resource
    private SpecificationsItemMapper specificationsItemMapper;
    @Override
    @Transactional
    public QDResult save(Specifications specifications) {
        specifications.setCreated(new Date());
        specifications.setUpdated(new Date());
        specifications.setStatus(1);
        if (specificationsMapper.insert(specifications)==1){
            for (SpecificationsItem item : specifications.getSpecificationsItems()){
                item.setSpecificationsId(specifications.getId());
                specificationsItemMapper.insert(item);
            }
            return QDResult.ok();
        }
        return QDResult.fal();
    }

    @Override
    @Transactional
    public QDResult edit(Specifications specifications) {
        if (specificationsMapper.updateById(specifications)==1){
            QueryWrapper<SpecificationsItem> wrapper = new QueryWrapper<>();
            wrapper.eq("specifications_id",specifications.getId());
            specificationsItemMapper.delete(wrapper);
            for (SpecificationsItem item : specifications.getSpecificationsItems()){
                item.setSpecificationsId(specifications.getId());
                specificationsItemMapper.insert(item);
            }
            return QDResult.ok();
        }
        return QDResult.fal();
    }

    @Override
    public QDResult delete(Specifications specifications) {
        if (specificationsMapper.updateById(specifications)==1){
            return QDResult.ok();
        }
        return QDResult.fal();
    }

    @Override
    public QDResult<Specifications> get(Long id) {
        Specifications specifications = specificationsMapper.selectById(id);
        if (specifications!=null){
            QueryWrapper<SpecificationsItem> wrapper = new QueryWrapper<>();
            wrapper.eq("specifications_id",id);
            List<SpecificationsItem> items = specificationsItemMapper.selectList(wrapper);
            specifications.setSpecificationsItems(items);
            return QDResult.ok(specifications);
        }
        return QDResult.notFound();
    }

    @Override
    public LayuiTableDTO<Specifications> query(int page, int limit, Specifications specifications) {
        LayuiTableDTO<Specifications> dto = new LayuiTableDTO<>();
        Page<Specifications> pager = new Page<>();
        pager.setCurrent(page);
        pager.setSize(limit);
        QueryWrapper<Specifications> wrapper = new QueryWrapper<>();
        // 组织查询条件
        if(specifications != null) {
            wrapper.like(specifications.getSpecificationsName()!=null && !specifications.getSpecificationsName().equals(""),"specifications_name", specifications.getSpecificationsName());
            wrapper.eq(specifications.getStatus()!=null && !specifications.getStatus().equals(0),"status",specifications.getStatus());
        }
        pager = specificationsMapper.selectPage(pager,wrapper);
        dto.setCount(pager.getTotal());
        dto.setData(pager.getRecords());
        return dto;
    }

    @Override
    public QDResult queryAll() {
        QueryWrapper wrapper = new QueryWrapper();
        return QDResult.ok(specificationsMapper.selectList(wrapper));
    }
}
